﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
public partial class zhuce : System.Web.UI.Page
{
    private static string sqlconn = System.Configuration.ConfigurationManager.AppSettings["strConn"].ToString().Trim();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Sid"] != null)
        {
            txtNum.Text = Session["id"].ToString().Trim();
        }
        if (!IsPostBack)
        {
            BindYear();
            BindMonth();
        }
    }
    void BindYear()
    {
        ddlYear.Items.Clear();
        int StarYear = DateTime.Now.Year - 30;
        int EndYear = DateTime.Now.Year;
        for (int i = StarYear; i <= EndYear; i++)
        {
            ddlYear.Items.Add(new ListItem(i.ToString()));
        }
        ddlYear.SelectedValue = EndYear.ToString();
    }

    void BindMonth()
    {
        ddlMonth.Items.Clear();
        for (int i = 1; i <= 12; i++)
        {
            ddlMonth.Items.Add(new ListItem(i.ToString()));
        }
    }
    protected void btnRegister_Click(object sender, EventArgs e)
    {
         string name = txtName.Text.Trim();
        string num = txtNum.Text.Trim();
        string password = txtPassword.Text.Trim();
        string sex = ddlSex.SelectedItem.Text.Trim();
        string year = ddlYear.SelectedItem.Text.Trim();
        string month = ddlMonth.SelectedItem.Text.Trim();
       
        if (month.Length == 1)
        {
            month = "0" + month;
        }
        string birthday = year + month;
        users us = new users();
        us.name = name;
        us.ps = password;
        us.birthday = birthday;
        us.id = num;
        us.sex = sex;
        int result = us.isExist(us);
        SqlConnection conn = new SqlConnection(sqlconn);
        try
        {
            conn.Open();
            string sql = "insert into UserInfo (UserID,UserName,UserPW,UserBirthday,UserSex) VALUES ('" + num + "','" + name + "','" + password + "','" + birthday + "','" + sex + "')";
            SqlCommand comm = new SqlCommand(sql, conn);
            comm.ExecuteNonQuery();
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            conn.Close();
        }
        if (result > 0)
        {
            Session["Sid"] = num;
            Response.Write("<script> alert(\"注册失败！\")</script>");
        }
        else
        {
            Response.Write("<script>alert(\"注册成功！\")</script>");
            Response.Redirect("~/denglu.aspx");

        }
    }
    
}